﻿using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.VisualElements;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPC.WinForm.ViewModel
{
    public class AngularViewModel:ObservableObject
    {
        public AngularViewModel(string name)
        {
            var sectionsOuter = 130;
            var sectionsWidth = 20;


            Needle = new NeedleVisual
            {
                Value = 0
            };

            Series = GaugeGenerator.BuildAngularGaugeSections(
                new GaugeItem(60, s => SetStyle(sectionsOuter, sectionsWidth,name,SKColors.CadetBlue, s)),
               new GaugeItem(30, s => SetStyle(sectionsOuter, sectionsWidth, name, SKColors.Yellow, s)),
            new GaugeItem(10, s => SetStyle(sectionsOuter, sectionsWidth, name, SKColors.Red, s)) ); 


            VisualElements = new VisualElement<SkiaSharpDrawingContext>[]
            {
                new AngularTicksVisual
                {
                    LabelsSize = 16,
                    LabelsOuterOffset = 15,
                    OuterOffset = 65,
                    TicksLength = 10
                },
                Needle
            };
        }

        public void UpdateValue(double value)
        {
            Needle.Value = value;
        }

        public IEnumerable<ISeries> Series { get; set; }

        public IEnumerable<VisualElement<SkiaSharpDrawingContext>> VisualElements { get; set; }

        public NeedleVisual Needle { get; set; }

        private static void SetStyle(double sectionsOuter,double sectionsWidth,string name,SKColor sKColor,PieSeries<ObservableValue> series)
        {
            series.Name = name;
            series.OuterRadiusOffset = sectionsOuter;
            series.MaxRadialColumnWidth = sectionsWidth;
            series.Fill = new SolidColorPaint(sKColor);
        }
    }
}
